package com.easyhouse.controller.controller;


import com.easyhouse.common.pojo.User;
import com.easyhouse.common.utils.CookieUtils;
import com.easyhouse.common.utils.VerifyCode;
import com.easyhouse.common.vo.SysResult;
import com.easyhouse.controller.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/user/manage")
public class UserController {
    @Autowired(required = false)
    private UserService userService;
    @Autowired(required = false)
    private StringRedisTemplate redisTemplate;
    //检查,注册表单中的用户名是否存在
    //存在,不可以使用,不存在,可以使用
    @RequestMapping("/checkUserName")
    public SysResult checkUsername(String userName){
        boolean available=userService.checkUsername(userName);
        //available true 可用 false不可用
        if(available){
            return SysResult.ok();
        }else{
            return SysResult.build(
                    201,"不可用",
                    null);
        }
    }
    //注册用户
    /*@InitBinder
    public void myInitBinder(DataBinder dataBinder){
        dataBinder.addValidators(new UserValidator());
    }*/
    @RequestMapping("/save")
    public SysResult doRegister(@Valid User user, Errors errors,@RequestParam("valistr") String valistr2){
        //校验验证码
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String valistr = opsForValue.get("valistr");
        if(valistr==null || valistr2 == null || !valistr.equals(valistr2)){
            return SysResult.build(201,"验证码不正确",null);
        }
        //校验数据
        if (errors.hasErrors()){
            String msg = errors.getFieldError().getDefaultMessage();
            return SysResult.build(201,msg,null);
        }
        try{
            userService.doRegister(user);
            SysResult ok = SysResult.ok();
            System.out.println(ok);
            return ok;

        }catch(Exception e){
            e.printStackTrace();
            return SysResult.build(
                    201,
                    "注册失败",
                    null);
        }
    }
    //登录接口
    @RequestMapping("/login")
    public SysResult doLogin(User user,
                             HttpServletResponse response,
                             HttpServletRequest request){
        //业务层service处理对redis的写操作,登录成功才写
        //才有key值 ticket
        String ticket=userService.doLogin(user);
        //ticket是否为空,判断登录成功失败,返回sysResult对象
        if(StringUtils.isNotEmpty(ticket)){
            //利用工具类CookieUtils 设置cookie值
            CookieUtils.setCookie
                    (request,response,
                            "EM_TICKET",ticket);
            //不空,登录校验成功
            return SysResult.ok();
        }else{
            return SysResult.build(
                    201,
                    "登录错误",
                    null);
        }
    }
    //获取登录用户信息
    @RequestMapping("/query/{ticket}")
    public SysResult queryUserJson(@PathVariable String ticket,
                                   HttpServletRequest request,
                                   HttpServletResponse response){
        String userJson=
                userService.queryUserJson(ticket);
        //如果超时了,ticket存在,userJson不会返回 null
        if(StringUtils.isEmpty(userJson)){
            //获取失败,没有登录信息,一定是登录超时了
            //ticket从请求中删除,客户端
            CookieUtils.deleteCookie(request,response,
                    "EM_TICKET");
            return SysResult.build(201,"超时了",null);
        }else{
            return SysResult.build(200,"获取成功",userJson);
        }

    }
    //登出功能
    @RequestMapping("/logout")
    public SysResult doLogout(HttpServletResponse response,
                              HttpServletRequest request){
        //拿到ticket
        String em_ticket =
                CookieUtils.getCookieValue(request, "EM_TICKET");
        //cookie删除EM_TICKET
        CookieUtils.deleteCookie(request,response,
                "EM_TICKET");
        //删除redis
        userService.doLogout(em_ticket);
        return SysResult.ok();
    }

    @RequestMapping("/queryUserId")
    public String queryByUserId(@RequestParam("userId") String userId){
        User user = userService.queryByUserId(userId);
        return user.getUserName();
    }

    /**
     * 生成验证码
     */
    @RequestMapping("user_ajax/verifyCode")
    public void valiImg(HttpServletResponse response) throws IOException {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        //--禁止缓存
        /*response.setDateHeader("Expires",-1);
        response.setHeader("Cache-Control","no-cache");
        response.setHeader("Pragma","no-cache");*/
        //--生成验证码
        VerifyCode vc = new VerifyCode();
        //--输出验证码到浏览器
        vc.drawImage(response.getOutputStream());
        //--将验证码值存入session
        String valistr = vc.getCode();
        //--打印验证码
        System.out.println(valistr);
        opsForValue.set("valistr",valistr);
    }


}

